package com.jsls.netspri.controller.login;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.jsls.netspri.dao.UserDao;
import com.jsls.netspri.entity.User;


@SessionAttributes({"Admin","User"})
@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Resource
	private UserDao userDao;
	
	//转到登录界面
	@RequestMapping(value = "/tologin", method = RequestMethod.GET)
	public String toLogin() {
		return "login/login";
	}

	//检查用户名和密码是否正确
	@RequestMapping(value = "/check", method = RequestMethod.GET)
	@ResponseBody
	public String checkLogin(@RequestHeader("username") String username,
			@RequestHeader("password") String password, Model model) {

		if(username.equals("liuq")&&password.equals("238857")){
			model.addAttribute("Admin", "liuq");
			return "1";
		}
		
		User user = userDao.findByName(username);
		if (user != null) {
			if (user.getPassword().equals(password)) {
				model.addAttribute("User", user);
				return "2";
			}
		}
		
		return "0";
		
	}
}
